package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.model.beans.Taste;
import com.example.coffeeseller_2.utils.BitmapUtil;
import com.example.coffeeseller_2.utils.DensityUtil;

import java.util.List;

/**
 * Created by WH on 2018/7/30.
 */

public class SeekBarView extends android.support.v7.widget.AppCompatSeekBar {

    private Context context;
    private AttributeSet attrs;
    private int thumbWidth, thumbHeight;
    private Paint paint;
    private List<Taste> tastes;
    private int canDragMaxIndex, curDragIndex = 0;

    public SeekBarView(Context context) {
        super(context);
    }

    public SeekBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.attrs = attrs;
        init();
    }

    public SeekBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        getDrawableSize();
        TypedArray attrsArray = context.obtainStyledAttributes(attrs, R.styleable.SeekBarView);
        try {
            thumbWidth = DensityUtil.dp2px(context, (int) attrsArray.getDimension(R.styleable.SeekBarView_thumbWidth, thumbWidth));
            thumbHeight = DensityUtil.dp2px(context, (int) attrsArray.getDimension(R.styleable.SeekBarView_thumbHeight, thumbHeight));
        } finally {
            attrsArray.recycle();
        }
        Drawable drawable = BitmapUtil.getDefineDrawable(getResources(), getThumb(), thumbWidth, thumbHeight);
        setPadding(drawable.getIntrinsicWidth() / 2, 0, drawable.getIntrinsicWidth() / 2, 0);
        setThumb(drawable);
        paint = new Paint();
        paint.setColor(ContextCompat.getColor(context, R.color.white));
    }

    private int measureSize(int measureSize) {
        int sizeMode = MeasureSpec.getMode(measureSize);
        int size = MeasureSpec.getSize(measureSize);
        if (sizeMode != MeasureSpec.EXACTLY) {
            return measureSize;
        } else {
            return size;
        }
    }

    private void getDrawableSize() {

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;

        BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, options);

        thumbWidth = options.outWidth;
        thumbHeight = options.outHeight;

    }


    public void setDatas(List<Taste> tastes) {

        this.tastes = tastes;
        invalidate();
    }

    public void setMaxIndex(int maxIndex) {
        canDragMaxIndex = maxIndex;
    }

    public void setCurDragIndex(int curDragIndex) {
        this.curDragIndex = curDragIndex;
        invalidate();
    }

    public int getCurDragIndex() {
        return curDragIndex;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(measureSize(widthMeasureSpec), thumbHeight);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*if (tastes != null && tastes.size() > 0) {
            int thumbWidth = getThumb().getIntrinsicWidth();
            int count = tastes.size();
            int avgLength = (getWidth() - thumbWidth) / ((count - 1) <= 0 ? 1 : (count - 1));

            for (int i = 0; i < curDragIndex; i++) {

                Log.d("------------","-----------------");

                canvas.drawCircle(thumbWidth / 2 + i * avgLength, getHeight() / 2, 10, paint);
            }
        }*/


    }


    /* @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (canDragMaxIndex == 0) {
            return false;
        }

        float x = event.getX() - getX();
        if (x < 0) {
            x = 0;
            return false;
        } else if (x > canDragMaxIndex * TradeLeftInfoViewHelper.avgLength) {
            x = canDragMaxIndex * TradeLeftInfoViewHelper.avgLength;
            return false;
        } else {

             return true;
        }
       *//* MotionEvent me = MotionEvent.obtain(event.getDownTime(), event.getEventTime(),
                event.getAction(), x, getTop() + getHeight() / 2, event.getMetaState());

        return onTouchEvent(me);*//*
    }*/
}
